<?php
namespace App\Http\Controllers\Api\FlyGo;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Goods\GoodsSale;
use App\Models\Mall\MallMaster;
use App\Models\ConfigureFunction;
use App\Service\PromotionService;
use App\Models\Goods\GoodsStore;
use App\Models\Goods\GoodsMaster;
use App\Models\Goods\GoodsMarktingStrategy;

class GoodsController extends Controller
{

    /**
     * 查询商品详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function search( Request $request )
    {

        //获取参数
        $barcode = $request->input('barcode');
        if (empty($barcode)) {
            return response()->json(['code' => 10001, 'message' => '缺少barcode参数']);
        }

        $session_id = $request->input('session_id');
        if (empty($session_id)) {
            return response()->json(['code' => 10002, 'message' => '缺少session_id参数']);
        }

        $pcustID = get_customer_id($session_id, 6);
        if (!ebsig_is_int($pcustID)) {
            return response()->json(['code' => 10003, 'message' => '登录已失效']);
        }

        $shop_id = $request->input('mallID');
        if (empty($shop_id)) {
            $shop_id = 0;
        }

        //查询门店设置
        $is_choose_goods_by_mall = ConfigureFunction::getConfigureField(8, 'is_choose_goods_by_mall');
        if ($is_choose_goods_by_mall == 1 && empty($shop_id)) {
            return response()->json(['code' => 10004, 'message' => '缺少shop_id参数']);
        }

        //获取区域id
        $mallareaID = 0;
        $mall_master = MallMaster::find($shop_id);
        if ($mall_master) {
            $mallareaID = $mall_master->mallareaID;
        }

        //检查商品信息是否存在
        $goodsSale = GoodsSale::getBarcodeCache($barcode, $shop_id);
        if (!$goodsSale) {
            return response()->json(['code' => 10005, 'message' => '商品信息不存在']);
        }

        if ($goodsSale['forsale'] != 1) {
            return response()->json(['code' => 10006, 'message' => '该商品已下架']);
        }

        //查询主商品信息
        $goodsMaster = GoodsMaster::find($goodsSale['mainPostID']);
        if (!$goodsMaster) {
            return response()->json(['code' => 10007, 'message' => '商品信息不存在']);
        }

        //查询商品库存
        if ($is_choose_goods_by_mall == 1) {

            $enable_sale_amount = GoodsStore::search($goodsSale['product_code'], $goodsSale['product_spec'], $shop_id);

            //门店销售策略表
            $goods_marketing_strategy = GoodsMarktingStrategy::where(['shop_id' => $shop_id, 'postID' => $goodsSale['postID']])->first();
            if ($goods_marketing_strategy) {
                $goods_marketing_strategy = $goods_marketing_strategy->toArray();
                $goodsSale['marketPrice'] = $goods_marketing_strategy['marketPrice'];
                $goodsSale['salePrice'] = $goods_marketing_strategy['salePrice'];
                $goodsSale['salePoints'] = $goods_marketing_strategy['salePoints'];
                $goodsSale['price'] = round($goods_marketing_strategy['salePrice'], 2);
            }

        } else {
            $enable_sale_amount = GoodsStore::search($goodsSale['product_code'], $goodsSale['product_spec']);
        }

        if ( $enable_sale_amount < 0 || $goodsSale['salePrice'] <= 0 ) {
            $enable_sale_amount  = 0;
        }

        //查询促销信息
        $goodsSale['promotion'] = [];
        $goodsSale['order_promotion'] = [];
        $goodsSale['price'] = $goodsSale['salePrice'];
        $goods_promotion = PromotionService::goods([
            'postID' => $goodsSale['postID'],
            'salePrice' => $goodsSale['salePrice'],
            'salePoints' => $goodsSale['salePoints'],
            'product_code' => $goodsSale['product_code'],
            'marketPrice' => $goodsSale['marketPrice'],
            'goods_amount' => 1,
            'pcustID' => $pcustID,
            'mallareaID' => $mallareaID,
            'mallID' => $shop_id,
            'platform' => 0
        ]);

        if (isset($goods_promotion['promotion']) && !empty($goods_promotion['promotion'])) {
            $goodsSale['price'] = $goods_promotion['price'];
            foreach ($goods_promotion['promotion'] as $p) {
                if (isset($p['selected']) && $p['selected'] == 1) {
                    $goodsSale['promotion'][] = $p;
                }
            }
        }

        //订单促销
        $promotion_order_data = PromotionService::orders([
            'pcustID' => $pcustID,
            'mallareaID' => $mallareaID,
            'mallID' => $shop_id,
            'is_use_coupon' => 2,
            'platform' => 0
        ], [
            [
                'postID' => $goodsSale['postID'],
                'goods_amount' => 1,
                'price' => $goodsSale['price'],
                'type' => 1
            ]
        ]);

        if ($promotion_order_data && !empty($promotion_order_data)) {
            $goodsSale['order_promotion'] = $promotion_order_data;
        }

        $return_data = [
            'img_link' => $request->getSchemeAndHttpHost() . $goodsMaster['goodsMainPic'],
            'postID' => $goodsSale['postID'],
            'goodsName' => $goodsSale['goodsName'],
            'price' => $goodsSale['price'],
            'salePrice' => $goodsSale['salePrice'],
            'marketPrice' => $goodsSale['marketPrice'],
            'enable_sale_amount' => $enable_sale_amount,
            'promotion' => $goodsSale['promotion'],
            'order_promotion' => $goodsSale['order_promotion']
        ];

        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $return_data]);
    }

}